﻿// Regist.cpp: 实现文件
//

#include "pch.h"
#include "ChatSystem.h"
#include "afxdialogex.h"
#include "Regist.h"
#include "ChatMsg.h"
#include "MsgQueue.h"
#include "TcpSvr.h"


// Regist 对话框

IMPLEMENT_DYNAMIC(Regist, CDialogEx)

Regist::Regist(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_REGIST, pParent)
	, sc_nickname(_T(""))
	, sc_school(_T(""))
	, sc_telnum(_T(""))
	, sc_passwd(_T(""))
{

}

Regist::~Regist()
{
}

void Regist::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDITNICKNAME, sc_nickname);
	DDX_Text(pDX, IDC_EDITSCHOOL, sc_school);
	DDX_Text(pDX, IDC_EDITTELNUM, sc_telnum);
	DDX_Text(pDX, IDC_EDITPASSWD, sc_passwd);
}


BEGIN_MESSAGE_MAP(Regist, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &Regist::OnBnClickedButton1)
END_MESSAGE_MAP()


// Regist 消息处理程序


void Regist::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//1.获取用户输入的最新消息
	UpdateData(TRUE);
	if (sc_telnum.IsEmpty() || sc_passwd.IsEmpty() || sc_nickname.IsEmpty() || sc_passwd.IsEmpty()) {
		MessageBox("输入内容不能为空");
		return;
	}
	ChatMsg cm;
	cm.msg_type_ = Register;
	cm.json_msg_["telnum"] = sc_telnum.GetString();
	cm.json_msg_["passwd"] = sc_passwd.GetString();
	cm.json_msg_["nickname"] = sc_nickname.GetString();
	cm.json_msg_["school"] = sc_passwd.GetString();
	//2.组织内容
	std::string msg;
	cm.GetMsg(&msg);

	//3.发送
	TcpSvr* ts = TcpSvr::GetInstance();
	if (ts == NULL) {
		return;
	}
	ts->Send(msg);

	//4.获取结果分析
	MsgQueue* mq = MsgQueue::GetInstance();
	msg.clear();
	cm.clear();
	mq->Pop(Register_Resp, &msg);
	cm.PraseChatMsg(0, msg);
	if (cm.reply_status_ == REGISTER_SUCCESS) {
		MessageBox("注册成功");
		CDialog::OnCancel();
	}
	else {
		MessageBox("注册失败");
	}

}
